package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.atguigu.jxc.util.PageResult;
import com.atguigu.jxc.util.QueryPageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: fqy
 * Date: 2021/10/15
 * Time: 19:11
 * Describe:
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    @Override
    public PageResult getCustomerPage(Integer page, Integer rows, String customerName) {
        //使用分页插件 得到page分页所需要的参数 有个线程 ThreadLocal
        PageHelper.startPage(page, rows);
        //获取分页条件 传给Mapper
        //有个监听器
        //根据condition和page数据调用Mapper查询数据库中的数据
        List<Customer> customerPageList = customerDao.selectCustomerPageByName(customerName);
        // 将结果封装到PageInfo对象中
        PageInfo<Customer> customerPageInfo = new PageInfo<>(customerPageList);
        PageResult pageResult = new PageResult(customerPageInfo.getTotal(), customerPageInfo.getList());
        return pageResult;
    }

    @Override
    public int updateCustomer(Customer customer, Integer customerId) {
        int i = customerDao.updateCustomer(customer,customerId);
        System.out.println("i = " + i);
        return i;
    }

    @Override
    public int addCustomer(Customer customer) {
        int i = customerDao.addCustomer(customer);
        System.out.println("i = " + i);
        return i;
    }

    @Override
    public int deleteCustomerBatchByIds(String[] arr) {
        int i = customerDao.deleteCustomerBatchByIds(arr);
        return i;
    }
}
